package yawning.mew.bear;

import yawning.mew.sim.SimulationAction;
import yawning.mew.sim.SimulationEventAction;

public class BarkskinAction extends SimulationEventAction {
	BearThreatSimulator mState;
	double mCooldownExpireTime;
	private boolean mTriggeredBonus;

	BarkskinAction(BearThreatSimulator state) {
		super(state.mSimEngine);
		mState = state;
	}

	@Override
	public double perform() {
		if (isUp() || mCooldownExpireTime > mState.mSimEngine.getSimTime()) return SimulationAction.ACTION_FAIL;
		if (mState.mIsLogging) mState.mLog.log("Barkskin\n");

		mState.mModel.multiplyIncPhysicalDamage(mState.mModel.multBarkskinDR);
		mState.mModel.multiplyIncMagicDamage(mState.mModel.multBarkskinDR);

		schedule(mState.mModel.barkskinBuffDuration);

		mCooldownExpireTime = mState.mSimEngine.getSimTime() + mState.mModel.barkskinCDDuration;

		return 0.0;
	}

	@Override
	public void onExpire() {


		if (mState.mModel.mToon.tier_12_4pc) {
			if (mTriggeredBonus) {
				mState.mModel.addDodge(-mState.mModel.tier_12_4pc_bonusDodge);
				mTriggeredBonus = false;

				m4T12UpDuration += mState.mSimEngine.getSimTime() - getScheduleTime();

				if (mState.mIsLogging) mState.mLog.log("Tier 12 4pc Expiration\n");
			} else {
				mState.mModel.multiplyIncPhysicalDamage(1.0f / mState.mModel.multBarkskinDR);
				mState.mModel.multiplyIncMagicDamage(1.0f / mState.mModel.multBarkskinDR);

				if (mState.mIsLogging) mState.mLog.log("Barkskin Expiration\n");

				mState.mModel.addDodge(mState.mModel.tier_12_4pc_bonusDodge);
				mTriggeredBonus = true;

				mBarkskinUpDuration += mState.mSimEngine.getSimTime() - getScheduleTime();

				schedule(mState.mModel.tier_12_4pc_buffDuration);

				if (mState.mIsLogging) mState.mLog.log("Tier 12 4pc\n");
			}
		} else {
			mState.mModel.multiplyIncPhysicalDamage(1.0f / mState.mModel.multBarkskinDR);
			mState.mModel.multiplyIncMagicDamage(1.0f / mState.mModel.multBarkskinDR);

			mBarkskinUpDuration += mState.mSimEngine.getSimTime() - getScheduleTime();

			if (mState.mIsLogging) mState.mLog.log("Barkskin Expiration\n");
		}
	}

	public boolean isBarkskinUp() {
		return isUp() && (!mTriggeredBonus);
	}

	public double getBarkskinUpTime() {
		if (isUp() && !mTriggeredBonus) {
			return mBarkskinUpDuration + mState.mSimEngine.getSimTime() - getScheduleTime();
		}

		return mBarkskinUpDuration;
	}

	public double get4T12UpTime() {
		if (isUp() && mTriggeredBonus) {
			return m4T12UpDuration + mState.mSimEngine.getSimTime() - getScheduleTime();
		}

		return m4T12UpDuration;
	}

	double mBarkskinUpDuration = 0;
	double m4T12UpDuration = 0;
}